/*
 * Copyright 2018-2023 guerlab.net and other contributors.
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.gnu.org/licenses/lgpl-3.0.html
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.guerlab.cloud.gateway.bodysecurity;

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

/**
 * 请求体安全信息监控端点.
 *
 * @author guer
 */
@Slf4j
@Endpoint(id = "body-security")
public class BodySecurityEndpoint {

	private final BodySecurityProperties properties;

	/**
	 * 创建实例.
	 *
	 * @param properties 请求体安全配置
	 */
	public BodySecurityEndpoint(BodySecurityProperties properties) {
		this.properties = properties;
	}

	/**
	 * 获取feign客户端实例信息列表.
	 *
	 * @return feign客户端实例信息列表
	 */
	@SuppressWarnings("unused")
	@ReadOperation
	public BodySecurityInfo get() {
		BodySecurityInfo info = new BodySecurityInfo();

		if (properties.getRsaKeys() != null) {
			info.setPublicKey(properties.getRsaKeys().getPublicKey());
		}

		List<PathSecurityInfo> configs = new ArrayList<>();
		for (PathSecurityConfig config : properties.getConfigs()) {
			PathSecurityInfo pathSecurityInfo = new PathSecurityInfo();
			pathSecurityInfo.setEnabledRequestSecurity(config.isEnabledRequestSecurity());
			pathSecurityInfo.setEnabledResponseSecurity(config.isEnabledResponseSecurity());
			pathSecurityInfo.setUrls(config.getUrls());
			if (config.getRsaKeys() != null) {
				pathSecurityInfo.setPublicKey(config.getRsaKeys().getPublicKey());
			}
		}

		return info;
	}
}
